<?php
/**
 * 库存挪用记录
 * (model)
 */

namespace App\Models\Inventory;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class Transfer extends Model
{
    protected $table = 'inventory_transfer';

    protected $guarded = [];

    /**
     * 站点 (出)
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function oArea(){
        return $this->belongsTo(SysArea::class,'oarea_id');
    }

    /**
     * 站点 (入)
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function iArea(){
        return $this->belongsTo(SysArea::class,'iarea_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static $_deductTransit = [
        0 => '否',
        1 => '是'
    ];

    public static $_status = [
        1 => '待审核',
        2 => '已通过',
        3 => '已驳回',
    ];

    public static function getDownloadData($re){
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $temp['psku_code'] = $row->sku->psku_code ?? '';
            $temp['season'] = ProductClassify::$_season[$row->sku->classify->season ?? 3];
            $temp['classify'] = $row->sku->classify->title ?? '';
            if(!array_key_exists($row->iuser_id,$user)){
                $user[$row->iuser_id] = User::getUsernameById($row->iuser_id);
            }
            $temp['iuser'] = $user[$row->iuser_id];
            $temp['iarea'] = $row->iArea->area ?? '';
            if(!array_key_exists($row->ouser_id,$user)){
                $user[$row->ouser_id] = User::getUsernameById($row->ouser_id);
            }
            $temp['ouser'] = $user[$row->ouser_id];
            $temp['oarea'] = $row->oArea->area ?? '';
            $temp['deduct_transit'] = self::$_deductTransit[$row->deduct_transit];
            $temp['transfer_num'] = $row->transfer_num;
            $temp['status'] = self::$_status[$row->status];
            $temp['created_at'] = $row->created_at;
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            yield $temp;
        }
    }
}
